// 本地存储
import 'dart:async';

import 'package:flutter/material.dart';
// import 'package:flutter_bugly_plugin/flutter_bugly_plugin.dart';
import 'package:whale/helper/utils/httputils/urlpath.dart';

class BuglyMan {
  BuglyMan._internal();
  static final BuglyMan _instance = BuglyMan._internal();

  factory BuglyMan() {
    return _instance;
  }

  Future<void> init() async {}

  void register<R>(R Function() body) async {
    // await FlutterBuglyPlugin.init(
    //     appIdAndroid: '711e2f7f78', appIdiOS: 'e242858e09');

    FlutterError.onError = _reportErrorAndLog;
    runZonedGuarded(
      body,
      (error, stack) {
        _makeDetails(error, stack);
      },
      zoneSpecification: ZoneSpecification(
        print: (self, parent, zone, line) {
          report(name: 'zoneSpecification', reason: line);
        },
      ),
    );
  }

  void _reportErrorAndLog(FlutterErrorDetails details) async {
    if (UrlPath.isDebug) {
      // In development mode simply print to console.
      FlutterError.dumpErrorToConsole(details);
    } else if (details.stack != null) {
      // In production mode report to the application zone to report to
      // Sentry.
      Zone.current.handleUncaughtError(details.exception, details.stack!);
    }

    report(
        name: details.exceptionAsString(),
        reason: details.summary.toDescription());
  }

  void _makeDetails(Object obj, StackTrace stack) async {
    // 构建错误信息

    report(name: stack.toString(), reason: obj.toString());
  }

  void report({
    required String name,
    required String reason,
    Map<String, String>? userInfo,
  }) {
    // userInfo必须是{String, String}, Android限制
    // FlutterBuglyPlugin.reportException(
    //   exceptionName: name,
    //   reason: reason,
    //   userInfo: userInfo,
    // );
  }
}
